<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="连接id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelId">
              <a-select v-model="model.channelId" @change="channelIdChange">
                <a-select-option :value="item.id" v-for="item in channelList" :key="item.id">{{ item.channelCode }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="包id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageInfoId">
              <a-select v-model="model.packageInfoId" show-search
                        optionFilterProp="children">
                <a-select-option :value="item.id" v-for="item in packageList" :key="item.packageCode">{{ item.packageCode }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="收发类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actionType">
              <j-dict-select-tag type="list" v-model="model.actionType" dictCode="actionType" placeholder="请选择收发类型" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.actionType === 'RECEIVE'">
            <a-form-model-item label="优先级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priority">
              <a-input-number v-model="model.priority" placeholder="请输入优先级" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.actionType === 'SEND'">
            <a-form-model-item label="自动发送" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="autoSend">
              <j-dict-select-tag placeholder="请选择自动发送" v-model="model.autoSend" dictCode="YN_CONSTANT"/>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'NmcChannelResolvePackageForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        channelList: [],
        packageList: [],
        model:{
          autoSend: "N"
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/channel/nmcChannelResolvePackage/add",
          edit: "/channel/nmcChannelResolvePackage/edit",
          queryById: "/channel/nmcChannelResolvePackage/queryById",
          channelList: "/channel/nmcChannelInfo/list",
          packageList: "/grid/nmcPackageInfo/list",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted() {
      this.getChannelList();
      this.getPackageList();
    },
    methods: {
      getChannelList() {
        getAction(this.url.channelList, {pageSize: -1}).then((res) => {
          if(res.success){
            this.channelList = res.result.records;
          }else{
            that.$message.warning(res.message);
          }
        });
      },
      getPackageList() {
        getAction(this.url.packageList, {pageSize: -1}).then((res) => {
          if(res.success){
            this.packageList = res.result.records;
          }else{
            that.$message.warning(res.message);
          }
        });
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      channelIdChange(value) {
        let find = this.channelList.find(item => item.id == value)
        this.model.channelCode = find.channelCode
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>